package com.xianbeiying.app.ckgl.activity;

import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;

import com.lidroid.xutils.util.LogUtils;
import com.xianbeiying.app.ckgl.util.NetUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.CustomDilog;

/**
 * 基础类
 */
public class BaseActivity extends FragmentActivity{
    final public static int CODE_CALL_PHONE = 1; //电话
    final public static int CODE_GET_CAMERA = 2; //相机
    final public static int CODE_EXTERNAL_STORAGE = 3; //存储空间
    final public static int CODE_ACCESS_COARSE_LOCATION = 4; //定位
    public static boolean isPermisson = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (isPermisson){
            getPermission();
        }
//        //开启定位
//        if (MyApplication.myMapLocation == null){
//            MyApplication.locationClient.startLocation();
//        }
        if (!NetUtil.isConnected(this)) {
            ToastUtil.showInfo(this, "您还未连接网络,请连接网络");
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume(){
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isPermisson = false;
    }

    /**
     * 6.0 权限适配
     * 方法说明: 已进入让用户选择给与权限 给本地存值来判断权限是否给与
     * 后面有用到权限 拿值判断
     */
    private void getPermission() {
        LogUtils.e("--------------------执行了");
        if (Build.VERSION.SDK_INT >= 23) { //如果是6.0
            int cameraPermission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA);
            if (cameraPermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{android.Manifest.permission.CAMERA,
                                android.Manifest.permission.CALL_PHONE,
                                android.Manifest.permission.READ_EXTERNAL_STORAGE,
                                android.Manifest.permission.ACCESS_COARSE_LOCATION}, CODE_GET_CAMERA);
                return;
            } else {
            }
        } else {
            //不是6.0
        }


    }

    /**
     * 拿到用户判断权限的回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    // 跳转系统设置界面
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case CODE_ACCESS_COARSE_LOCATION: //定位
                if (grantResults[3] == PackageManager.PERMISSION_GRANTED) {
                    //如果用户给与权限
                } else {
                    //                    SharedPreferencesUtils.setBooleanDate("getStorage",false);
                    CustomDilog.dialog(this, this);
                }
                break;
            case CODE_GET_CAMERA: //相机
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    //如果用户给与权限
                } else {
                    //                    SharedPreferencesUtils.setBooleanDate("getStorage",false);
                    CustomDilog.dialog(this, this);
                }
                break;

            case CODE_CALL_PHONE: //打电话
                if (grantResults[1] == PackageManager.PERMISSION_GRANTED) {

                    //如果用户给与权限
                } else {
                    //                    SharedPreferencesUtils.setBooleanDate("getStorage",false);
                    CustomDilog.dialog(this, this);
                }
                break;

            case CODE_EXTERNAL_STORAGE: //存储空间
                if (grantResults[2] == PackageManager.PERMISSION_GRANTED) {

                    //如果用户给与权限
                } else {
//                    SharedPreferencesUtils.setBooleanDate("getStorage",false);
                    CustomDilog.dialog(this, this);
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}
